package com.css.fxfzaqbz.modules.data.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess2.entity.UrgencysettleData;
import com.css.fxfzaqbz.modules.data.entity.DisposeExport;
import com.css.fxfzaqbz.modules.data.entity.DisposeManager;
import com.css.fxfzaqbz.modules.data.queryParams.ActMap;
import com.css.fxfzaqbz.modules.data.queryParams.ActReport;
import com.css.fxfzaqbz.modules.data.queryParams.DisposeManagerQueryParams;
import com.css.fxfzaqbz.modules.data.repository.DisposeExportRepository;
import com.css.fxfzaqbz.modules.data.repository.DisposeManagerRepository;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DisposeManagerRepositoryImpl implements DisposeManagerRepository {

    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    /**
     * 新增处置要点库
     */
    @Override
    public void saveDisposeManager(DisposeManager disposeManager) {
        String sql = "insert into sys_dispose_manager (dm_id,dm_type,dm_label,dm_subtitle,label_id,subtitle_id," +
                "disaster_risk,disposal_point,del_flag,create_user,create_time,upload_time,admin,ia_id,ia_name,task_name,flag) " +
                "values(:dmId,:dmType,:dmLabel,:dmSubtitle,:labelId,:subtitleId,:disasterRisk," +
                ":disposalPoint,:delFlag,:createUser,:createTime,:uploadTime,:admin,:iaId,:iaName,:taskName,:flag)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(disposeManager);
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
    }

    /**
     * 根据 id 获取出处置要点信息
     *
     * @param id 处置要点主键id
     * @return 处置要点信息
     */
    @Override
    public DisposeManager getDisposeManager(String id) {
        String sql = "select * from sys_dispose_manager where dm_id = '" + id + "'  and  del_flag= '" + YNEnum.N + "'";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(DisposeManager.class));
    }

    @Override
    public void delDisposeManager(List<String> ids, Timestamp timestamp, String userId) {
        String sql = "update sys_dispose_manager set del_flag = '" + YNEnum.Y + "' , update_time = '" + timestamp + "' , update_user ='" + userId + "'  where dm_id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public DisposeManager updateDisposeManager(DisposeManager disposeManager) {
        String sql = "update sys_dispose_manager set" +
                " dm_type = :dmType," +
                " dm_label = :dmLabel, " +
                " dm_subtitle = :dmSubtitle," +
                " label_id = :labelId," +
                " subtitle_id = :subtitleId," +
                " update_time = :updateTime, " +
                " update_user = :updateUser, " +
                " disaster_risk = :disasterRisk, " +
                " disposal_point = :disposalPoint " +
                " where dm_id = '" + disposeManager.getDmId() + "'";

        SqlParameterSource source = new BeanPropertySqlParameterSource(disposeManager);
        namedParameterJdbcTemplate.update(sql, source);
        return disposeManager;
    }

    @Override
    public Map<String,Object> getAllDisposeManagerParams(DisposeManagerQueryParams queryParams) {
        StringBuilder sql = new StringBuilder(" SELECT dm.* from (SELECT * FROM sys_dispose_manager where del_flag = '0' and ia_name ='系统默认' union " +
                "SELECT d.* FROM sys_dispose_manager d LEFT JOIN sys_important_activity ia on d.ia_id=ia.ia_id where d.del_flag='0' and ia.del_flag ='0' and ia.ia_activity_status = '1') dm");

        StringBuilder whereSql = new StringBuilder(" where dm.del_flag = '" + YNEnum.N.toString() + "'");

        if (!PlatformObjectUtils.isEmpty(queryParams.getDmLabel())) {
            whereSql.append(" and dm.dm_label like '%" + queryParams.getDmLabel() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getDmType())) {
            whereSql.append(" and dm.dm_type = '" + queryParams.getDmType() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getDmSubtitle())) {
            whereSql.append(" and dm.dm_subtitle like '%" + queryParams.getDmSubtitle() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getIaName())) {
            whereSql.append(" and dm.ia_name like '%" + queryParams.getIaName() + "%'");
        }
        StringBuilder orderSql = new StringBuilder(" order by dm.create_time desc");
        int firstIndex = (queryParams.getCurPage() - 1) * queryParams.getPageSize();
        int lastIndex = queryParams.getPageSize();
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        sql.append(whereSql).append(orderSql).toString();
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(DisposeManager.class)).size();
        List<DisposeManager> disposeManagers = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(DisposeManager.class));
        System.out.println(sql.toString());
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("disposeManagers", disposeManagers);
        return map;
    }

    @Override
    public Map<String, Object> getDisposeManagerByIaId(String id,int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from sys_dispose_manager where ia_id = '" + id + "'  and  del_flag= '" + YNEnum.N + "'");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        sql.append(orderSql).toString();
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(DisposeManager.class)).size();
        List<DisposeManager> disposeManagers = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(DisposeManager.class));
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("disposeManagers", disposeManagers);
        return map;
    }

    @Override
    public List<DisposeManager> getCzByIaId(String iaId) {
        String sql = "select * from sys_dispose_manager where ia_id='" + iaId + "' and del_flag='" + YNEnum.N + "' and flag='" + YNEnum.Y + "' order by update_time desc";
        List<DisposeManager> disposeManagers = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DisposeManager.class));
        return disposeManagers;
    }

    @Override
    public List<DisposeManager> getQcCzByIaId(String iaId) {
        String sql = "select * from sys_dispose_manager where ia_id='" + iaId + "' and del_flag='" + YNEnum.N + "' ";
        try {
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DisposeManager.class));
        } catch (DataAccessException e) {
           return null;
        }
    }

    @Override
    public List<DisposeManager> getCzByType(String lx, String iaId) {
        String sql = "select * from sys_dispose_manager where ia_id='" + iaId + "' and dm_type='"+lx+"' and del_flag='" + YNEnum.N + "' and flag='" + YNEnum.Y + "' ";
        try {
            List<DisposeManager> disposeManagers = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DisposeManager.class));
            return disposeManagers;
        } catch (DataAccessException e) {
           return null;
        }
    }

    @Override
    public List<DisposeManager> getAnalysisByFatherLabel(String dmLabel, String iaId,String type) {
        String sql = "select * from sys_dispose_manager where dm_label='" + dmLabel + "' and ia_id='"+iaId+"' and dm_type ='"+type+"'  and del_flag='" + YNEnum.N + "' ";
        try {
            List<DisposeManager> disposeManagers = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DisposeManager.class));
            return disposeManagers;
        } catch (DataAccessException e) {
            return null;
        }
    }


    @Override
    public void updateSubmitButton(List<String> ids, String userName, String mobilePhone, Timestamp currentTimestamp) {
        String sql = "update sys_dispose_manager set" +
                " submiter = '"+userName+"'," +
                " submit_phone = '"+mobilePhone+"', " +
                " flag = '"+YNEnum.Y+"'," +
                " update_time = '"+currentTimestamp+"'" +
                " where dm_id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);

    }


}
